<?php

namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

class SettingController extends Controller
{

    public function edit_profile() {
        $user = \Auth::user();
        $student = $user->student;
        $edit = false;
        return view('student.profile', compact("student", "user", "edit"));
    }

    public function store_profile(Request $request) {
        $this->validate($request, [
        'phone' => 'required|numeric',
        'email' => 'required|email',
        'class' => 'required|max:255',
        'major' => 'required|max:255',
        ]);


        $user = \Auth::user();
        $user->update(["phone" => $request->input("phone"),
                        "email" => $request->input("email"),
                    ]);
        $student = $user->student;
        $student->update(["class" => $request->input("class"),
                            "major" => $request->input("major")]);
        $edit = true;
        return view('student.profile', compact("student", "user", "edit"));
    }

    public function edit_password() {
        $edit = false;
        $user = \Auth::user();
        $student = $user->student;
        return view('student.password', compact("user", "student", "edit"));
    }

    public function store_password(Request $request) {
        $user = \Auth::user();
        $student = $user->student;

        $this->validate($request, [
            'password'=>'required|min:6|confirmed',
            'password_confirmation' => 'required|min:6'
        ]);

        $user->update(["password" => bcrypt($request->input("password"))]);

        $edit = true;
        return view('student.password', compact("user", "student", "edit"));
    }
}
